package com.jcfk.system.controller;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.controller.BaseController;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.web.domain.RetResult;
import com.jcfk.system.domain.dto.SysPermissionDTO;
import com.jcfk.system.service.SysPermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 菜单管理
 *
 * @author awei
 */
@Api(value = "/sysPermission", tags = {"菜单管理"})
@RestController
@RequestMapping("/sysPermission")
public class SysPermissionController extends BaseController {

    @Autowired
    private SysPermissionService permissionService;


    /**
     * 获取菜单树  用于角色分权权限的时候使用
     *
     * @return
     */
    @ApiOperation(value = "获取菜单树  用于角色分权权限的时候使用", notes = "获取菜单树  用于角色分权权限的时候使用", httpMethod = "POST")
    @PostMapping("roleTree")
    public RetResult<List<SysPermissionDTO>> roleTree() {
        return RetResult.ok(permissionService.listAll(null));
    }

    /**
     * 获取菜单树  用于维护管理权限使用
     *
     * @return
     */
    @ApiOperation(value = "获取菜单树  用于维护管理权限使用", notes = "获取菜单树  用于维护管理权限使用", httpMethod = "POST")
    @PostMapping("manage")
    public RetResult<List<SysPermissionDTO>> manage() {
        return RetResult.ok(permissionService.listAll(null));
    }

    /**
     * 获取菜单列表 用查询所有权限使用
     *
     * @return
     */
    @ApiOperation(value = "获取菜单列表  用查询所有权限使用", notes = "获取菜单列表 用查询所有权限使用", httpMethod = "POST")
    @PostMapping("list/{applicationType}")
    public RetResult<List<SysPermissionDTO>> list(@PathVariable("applicationType") String applicationType) {
        return RetResult.ok(permissionService.list(applicationType,"1"));
    }

    /**
     * 添加/更新管理菜单
     *
     * @param dto
     * @return
     */
    @ApiImplicitParams({@ApiImplicitParam(paramType = "body", dataType = "SysPermissionDTO", name = "dto", value = "", required = true)})
    @ApiOperation(value = "添加/更新管理菜单", notes = "添加/更新管理菜单", httpMethod = "POST")
    @RequestLogHeader(modular = "权限管理", businessType = "保存")
    @PostMapping("/save")
    @ResponseBody
    public RetResult<?> save(@NonNull @RequestBody @Valid SysPermissionDTO dto) {

        // 验证权限Code是否重复
        RetResult ret = permissionService.isValid(dto);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        if (StrUtil.isEmpty(dto.getId())) {
            // 新增
            return RetResult.ok(permissionService.insert(dto));
        } else {
            // 更新
            return RetResult.ok(permissionService.update(dto));
        }
    }

    /**
     * 添加/更新管理菜单
     *
     * @param dto
     * @return
     */
    @ApiImplicitParams({@ApiImplicitParam(paramType = "body", dataType = "SysPermissionDTO", name = "dto", value = "", required = true)})
    @ApiOperation(value = "添加/更新管理菜单", notes = "添加/更新管理菜单", httpMethod = "POST")
    @RequestLogHeader(modular = "权限管理", businessType = "移动")
    @PostMapping("/move")
    @ResponseBody
    public RetResult move(@NonNull @RequestBody SysPermissionDTO dto) {
        permissionService.move(dto);
        return RetResult.ok();
    }


    /**
     * 逻辑删除权限
     *
     * @param dto
     * @return
     */
    @ApiImplicitParams({@ApiImplicitParam(paramType = "body", dataType = "SysPermissionDTO", name = "sysUser", value = "", required = true)})
    @ApiOperation(value = "逻辑删除权限", notes = "逻辑删除权限", httpMethod = "POST")
    @RequestLogHeader(modular = "权限管理", businessType = "删除")
    @PostMapping("/delete")
    @ResponseBody
    public RetResult<?> delete(@NonNull @RequestBody SysPermissionDTO dto) {
        return permissionService.logicDelete(dto);
    }


    /**
     * 根据id获取权限信息
     *
     * @param id
     * @return
     */
    @ApiImplicitParams({@ApiImplicitParam(paramType = "path", dataType = "string", name = "id", value = "", required = true)})
    @ApiOperation(value = "根据id获取权限信息", notes = "根据id获取权限信息", httpMethod = "GET")
    @GetMapping("/get/{id}")
    @ResponseBody
    public RetResult<SysPermissionDTO> get(@NonNull @PathVariable("id") String id) {

        return RetResult.ok(permissionService.get(id));
    }
}
